//index.js
//获取应用实例
var app = getApp()
Page({
    data: {
        news: []
    },
    //新闻详情点击
    bindViewTapNewsArticle: function (event) {
        app.globalData.article = event.currentTarget.dataset.article;
        console.log(app.globalData.article)
        wx.navigateTo({
            url: '../article/article'
        })
    },
    fetchNews: function (isForceRefresh) {

        var that = this;
        var appNews = app.globalData.news;
        if (appNews.length < 1 || isForceRefresh) {
            wx.showLoading({
                title: '刷新资讯',
            })
            //获取资讯数据
            wx.request({
                url: "https://www.mojotv.cn/wx-api/article/index/0/16",
                success: function (res) {
                    that.setData({
                        news: res.data
                    });
                    app.globalData.news = res.data;
                    wx.hideLoading()

                }
            });
        } else {
            this.setData({
                news: appNews
            });
        }
    },
    onLoad: function () {

        this.fetchNews(false);

    },

    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function () {
        //刷新数据

        this.fetchNews(false);

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        wx.showLoading({
            title: 'HBO美剧正在加载更多资讯...',
        })
        //下拉加载更多
        var that = this;
        var appNews = app.globalData.news;
        //获取资讯数据
        wx.request({
            url: "https://www.mojotv.cn/wx-api/article/index/" + appNews.length + "/3",
            success: function (res) {
                var merged_news = appNews.concat(res.data)
                that.setData({
                    news: merged_news
                });
                app.globalData.news = merged_news;
                wx.hideLoading()
            },
            fail: function () {
                wx.hideLoading()
                wx.showToast({
                    title: '加载更多失败,请重试',
                    icon: 'success',
                    duration: 2000
                })
            }
        });
    },
})
